package com.hoioy.findjob;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays;

//java基础数据类型
public class ServerSCTest {
    public static void main(String[] args) throws IOException {
        serversocketbufarray();
    }

    public static void serversocketbufarray() throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(7000));

        ByteBuffer[] byteBuffers = new ByteBuffer[]{ByteBuffer.allocate(5), ByteBuffer.allocate(3)};

        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            long bytesRead = socketChannel.read(byteBuffers);
            while (bytesRead > 0) {
                Arrays.asList(byteBuffers).forEach(buf -> {
                    buf.flip();
                    while (buf.hasRemaining()) {
                        System.out.print((char) buf.get());
                    }
                    System.out.println();
                    buf.clear();
                });
                bytesRead = socketChannel.read(byteBuffers);
            }
        }
    }


    public static void serversocketbuf() throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(7000));

        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            ByteBuffer buf = ByteBuffer.allocate(48);
            int bytesRead = socketChannel.read(buf);
            while (bytesRead > 0) {
                buf.flip();
                while (buf.hasRemaining()) {
                    System.out.print((char) buf.get());
                }
                System.out.println();
                buf.clear();
                bytesRead = socketChannel.read(buf);
            }
        }
    }

}
